﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using android.bluebox.model;

namespace MobileVM
{
    public partial class MainForm : Form
    {
        private String _ip = "192.168.1.8";
        private int _port = 7777;
        
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            String sessionID = "123";
            String[] msgType = { "***", sessionID+"$2872$KhangVM$b$a", sessionID+"$"+Constant.REQUESTPAIRCODE+"$b$a" };
            foreach (String type in msgType)
            {
                cmbTypeMsg.Items.Add(type);
            }
        }


        public void sendMessage(String str)
        {            
            byte[] bytes = new byte[1024];
            try
            {
                IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
                IPAddress ipAddress = ipHostInfo.AddressList[0];
                //IPAddress ipAddress = IPAddress.Parse(_ip);
                IPEndPoint remoteEP = new IPEndPoint(ipAddress, _port);
                //MessageBox.Show(ipAddress.ToString() + "__" + _port.ToString());

                Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                try
                {
                    sender.Connect(remoteEP);
                    Console.WriteLine("Socket connected to {0} ", sender.RemoteEndPoint.ToString());
                    if (str == null)
                        str = "123$2872$KhangVM$b$a";
                    if (chkBoxEncrypt.Checked == true)
                    {
                        Crypto31.createInstance("123");
                        str = Crypto31.encrypt(str);
                    }
                    str += "<EOF>";
                    byte[] msg = Encoding.ASCII.GetBytes(str);
                    int byteSent = sender.Send(msg);
                    txtMsgs.Text += "Sending: "+str+"\r\n";

                    sender.Shutdown(SocketShutdown.Both);
                    sender.Close();
                    //Console.Read();
                }
                catch (ArgumentNullException ane)
                {
                    Console.WriteLine("ArgumentNullException: {0}", ane.ToString());
                }
                catch (SocketException se)
                {
                    Console.WriteLine("SocketException: {0}", se.ToString());
                }
                catch (Exception e)
                {
                    Console.WriteLine("Unknown exception: {0}", e.ToString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: {0}", e.ToString());
            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (cmbTypeMsg.SelectedIndex == 0 || cmbTypeMsg.SelectedIndex == -1)
            {
                sendMessage(txtSendMsg.Text);
            }
            else
            {
                sendMessage(cmbTypeMsg.SelectedItem.ToString());
            }
        }


    }
}
